package controllers;

import com.et.ar.exception.ActiveRecordException;
import com.et.mvc.JsonView;
import java.util.*;

import model.SelectedSubMenus;
import model.Sys_priv_menu;
import model.Sys_sub_menu;
import model.User_list;

public class Sys_priv_menuController extends ApplicationController {
	/**
	 * 返回全部功能
	 */
	public JsonView getSys_priv_menus() throws Exception {
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		long total = Sys_priv_menu.count(Sys_priv_menu.class, cond, args); // 查询单位总数
		List<Sys_priv_menu> Sys_priv_menus = Sys_priv_menu.findAll(Sys_priv_menu.class); // 查询单位的一页资料

		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", Sys_priv_menus);
		return new JsonView(result);
	}

	public JsonView getSys_priv_menu(int id) throws Exception {
		Sys_priv_menu sys_priv_menu = Sys_priv_menu.find(Sys_priv_menu.class, id);
		return new JsonView(sys_priv_menu);
	}

	/**
	 * 新建及修改的功能
	 */
	public JsonView updateAndSave() throws Exception {
		String priv_no_id = request.getParameter("priv_no_id");
		String func_ids = request.getParameter("func_ids");
		// 先删除该角色下的功能
		List<Sys_priv_menu> l_rm = Sys_priv_menu.findAll(Sys_priv_menu.class, "priv_no=?",
				new Object[] { priv_no_id });
		for (Sys_priv_menu rm : l_rm) {
			rm.destroy();
		}
		if (func_ids != null && func_ids.length() > 0) {
			// 说明选择了功能
			if (func_ids.indexOf(':') != -1) {

				// 说明有多个功能
				String ids[] = func_ids.split(":");
				for (int i = 0; i < ids.length; i++) {
					// 角色priv_no_id下的功能id
					Integer id = Integer.parseInt(ids[i]);
					// 新增
					Sys_priv_menu rm = new Sys_priv_menu();
					rm.setPriv_no(Integer.parseInt(priv_no_id));
					rm.setSys_sub_menu_id(id);
					rm.save();
				}
			} else {
				// 说明只有一个功能
				Sys_priv_menu rm = new Sys_priv_menu();
				rm.setPriv_no(Integer.parseInt(priv_no_id));
				rm.setSys_sub_menu_id(Integer.parseInt(func_ids));
				rm.save();
			}
		}

		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	/*
	 * 查询所有的子功能，并根据角色ID将该ID下的功能ck字段选上
	 */
	public JsonView getSubMenusByRoleId(int rows, int page, Integer id)
			throws ActiveRecordException {
		// 根据角色ID查询出所有该ID下的功能ID

		User_list user = (User_list)session.getAttribute("sessionAccount");
		String company = user.getUser_company();
		String cond1 = "SELECT spm.* FROM sys_priv_menu spm INNER JOIN sys_priv sp ON spm.priv_no=sp.id AND sp.company_id="+id+" AND priv_name LIKE 'admin_%' where 1=1 ";
		List<Object> tmpArgs1 = new ArrayList<Object>();
		
//		System.out.println(cond1+"  "+id); 
		Object[] args1 = tmpArgs1.toArray();
		//long total1 = Sys_priv_menu.count(Sys_priv_menu.class, cond1, args1);
		List<Sys_priv_menu> Sys_priv_menus = Sys_priv_menu.findBySql(Sys_priv_menu.class, cond1,
				args1);

		// 查询所有的功能
		String cond2 = "SELECT ssm.* FROM sys_priv_menu spm INNER JOIN sys_priv sp ON spm.priv_no=sp.id AND sp.company_id="+company+" AND priv_name LIKE 'admin_%' INNER JOIN sys_sub_menu ssm ON spm.sys_sub_menu_id=ssm.id and ssm.status=0";
		String cond2count = "SELECT count(ssm.id) count FROM sys_priv_menu spm INNER JOIN sys_priv sp ON spm.priv_no=sp.id AND sp.company_id="+company+" AND priv_name LIKE 'admin_%' INNER JOIN sys_sub_menu ssm ON spm.sys_sub_menu_id=ssm.id and ssm.status=0";
//		System.out.println(cond2);
		List<Object> tmpArgs2 = new ArrayList<Object>();
		Object[] args2 = tmpArgs2.toArray();
		List<Map<String,Object>> count_map = Sys_sub_menu.getResultMap(Sys_sub_menu.class, cond2count ,null, "",0,Integer.MAX_VALUE); // 查询总数
		long total2 = count_map.size()>0?Long.parseLong(count_map.get(0).get("count").toString()):1;
		List<Sys_sub_menu> sub_menus = Sys_sub_menu.findBySql(Sys_sub_menu.class,
				cond2, args2, "id", rows, (page - 1) * rows); // 查询的一页资料

		List<SelectedSubMenus> ssms = new ArrayList<SelectedSubMenus>();

		for (Sys_sub_menu sms : sub_menus) {
			SelectedSubMenus ssm = new SelectedSubMenus();
			ssm.setCk("");
			for (Sys_priv_menu rms : Sys_priv_menus) {
				if (sms.getId().equals(rms.getSys_sub_menu_id())) {
					ssm.setCk("已选");// 有该功能
				}
			}
			ssm.setFunc_css(sms.getFunc_css());
			ssm.setFunc_name(sms.getFunc_name());
			ssm.setFunc_url(sms.getFunc_url());
			ssm.setId(sms.getId());
			ssm.setImage(sms.getImage());
			ssm.setSys_menuId(sms.getSys_menuId());
			ssm.setTab_name(sms.getTab_name());

			// 将selectedsubmenu对象放到list中
			ssms.add(ssm);
		}
		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total2);
		result.put("rows", ssms);
		return new JsonView(result);
	}
}
